//
//  PasswordViewController.m
//  OMS
//
//  Created by goldeney on 14-9-24.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "PasswordViewController.h"
#import "ExitLoginService.h"
#import "NSKeyedArchiverUserLogin.h"
#import "ServiceConnect.h"
#import "NSDictionary+DataFormat.h"
#import "AESCrypt.h"

@interface PasswordViewController ()

@property (nonatomic, strong) JGProgressHUD *HUD;

@end

@implementation PasswordViewController
@synthesize scrollView = _scrollView;
@synthesize userPasswdOld = _userPasswdOld;
@synthesize userPasswdOne = _userPasswdOne;
@synthesize userPasswdTwo = _userPasswdTwo;
@synthesize userPasswdBtn = _userPasswdBtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"登录密码修改"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _scrollView.contentSize = CGSizeMake(_scrollView.contentSize.width, 545);
    
    [self setTextFieldAttribute:_userPasswdOld];
    [self setTextFieldAttribute:_userPasswdOne];
    [self setTextFieldAttribute:_userPasswdTwo];
    [self setButtonAttribute:_userPasswdBtn];
}

- (void) setTextFieldAttribute:(UITextField*)textField {
    [textField setBorderStyle:UITextBorderStyleLine];
    textField.layer.borderWidth = 1.0;
    textField.layer.borderColor = ThemeColorBlue.CGColor;
}

- (void) setButtonAttribute:(UIButton*)button {
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 5.0;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)writeLoginOldPassword:(NSString*)oldPassword newPassword:(NSString*)newPassword
{
    //获取用户登录信息
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    
    ServiceParametModel *model = [[ServiceParametModel alloc] init];
    [model setValue:userInfo.Id forKey:@"userid"];
    [model setValue:oldPassword forKey:@"pwd"];
    [model setValue:newPassword forKey:@"newpwd"];
    
    ServiceConnect *content = [[ServiceConnect alloc] initWithMethodName:@"OA_Password_Change" dataArray:model];
    [content showMBProgressHUD:self.navigationController labelText:@"修改密码中"];
    [content startConnectNetworkCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //result:(0失败，1成功，2历史密码错误，3历史密码于新密码相同)
        if (result.resultArray && result.resultArray.count>0) {
            NSDictionary *mDictionary = [result.resultArray firstObject];
            NSInteger resultState = [[mDictionary objectFormatForKey:@"result"] integerValue];
            if (resultState == 1){//修改成功
                [self successModifPass];
            } else {
                NSString *error = [mDictionary objectFormatStringForKey:@"error"];
                [self showAlertView:error];
            }
        } else {
            [self showAlertView:@"密码修改失败，请重新尝试"];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        [self showAlertView:@"密码修改失败，请重新尝试"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        [self showAlertView:@"网络连接异常，请检查网络是否连接"];
    }];
}

#pragma mark - UIButton

- (IBAction)loginPasswordSelector:(id)sender
{
    if ([_userPasswdOld.text isEqualToString:@""]) {
        [self showAlertView:@"请输入旧登录密码"];
        
    } else if ([_userPasswdOne.text isEqualToString:@""]) {
        [self showAlertView:@"请输入登录密码"];
        
    } else if ([_userPasswdTwo.text isEqualToString:@""]) {
        [self showAlertView:@"请再次输入登录密码"];
        
    } else if (_userPasswdOne.text.length<6 || _userPasswdTwo.text.length<6 ) {
        [self showAlertView:@"新登录密码不能少于6位，请重新输入"];
        
    } else if (![_userPasswdOne.text isEqualToString:_userPasswdTwo.text]) {
        [self showAlertView:@"两次输入的新密码不相同，请重新输入"];
        
    } else if ([_userPasswdOne.text isEqualToString:_userPasswdOld.text]) {
        [self showAlertView:@"旧密码和新密码不能相同，请重新输入"];
        
    } else {
        //连接网络，拉取数据
        NSString *oldStringMd5 = [AESCrypt encrypt:_userPasswdOld.text];
        NSString *newStringMd5 = [AESCrypt encrypt:_userPasswdOne.text];
        [self writeLoginOldPassword:oldStringMd5 newPassword:newStringMd5];
    }
}

#pragma mark
#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //退出登录
    [ExitLoginService exitAppUserLogin];
}

#pragma mark - UIAlertView

- (void)successModifPass
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"密码修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

@end
